﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using DB.SqlHelper;
using Framework.Common.Common;
using Mode;

namespace Framework.DataAccess
{
    public class AreRoleNode
    {
        public are_table are;
        public List<are_role_table> roles = new List<are_role_table>();
    }

    public class AreRoleTree : AreRoleNode
    {
        private List<AreRoleNode> childNode = new List<AreRoleNode>();

        public AreRoleTree( int areid )
        {
            are = AreRoleManage.GetSelfAre(areid);
            if (null == are)
                throw new Exception("无法获取区域区域信息\n ARE_ID["+areid +"]");

            if (are.level == 0)
            {
                DbInstance db = DbManager.GetDbInstance();
                DataView view = db.ExecuteDataTableBySql("SELECT * FROM are_table WHERE level=1").DefaultView;
                for( int index=0; index<view.Count; index++)
                {
                    create_are_role_tree(new are_table(
                                    view[index]["id"].ToInt(),
                                    view[index]["name"].ToString(),
                                    view[index]["level"].ToInt()));
                }
            }
            
            roles.AddRange(AreRoleManage.GetRoleByAre(are.id));
            
        }


        private void create_are_role_tree( are_table are )
        {
            AreRoleNode node = new AreRoleNode();
            node.roles.AddRange(AreRoleManage.GetRoleByAre(are.id));
            node.are = new are_table(are.id, are.name, are.level);

            childNode.Add( node);
        }

        // is_role:  是否显示角色
        // is_public : True 显示公共帐号
        public string ToJson(bool is_role = true, bool is_stop=false, bool is_public = false, bool is_admin = false)
        {
            StringBuilder result = new StringBuilder();

            result.Append("{\"text\":\"" + are.name + "\" , \"areid\": \"" + are.id + "\" , \"value\": \"-1\" , \"children\":[");
            foreach (AreRoleNode item in childNode)
            {
                result.Append(node_json(item,is_role, is_stop, is_public, is_admin));
            }

            if (result[result.Length - 1] == ',') result.Remove(result.Length - 1, 1);
            result.Append("]}");

            // 有角色节点,并且需要显示角色
            if (roles.Count > 0 && is_role)
            {
                foreach (are_role_table item in roles)
                {
                    // 是否显示停用角色
                    if (!item.status && !is_stop)
                        continue;

                    // 是否显示公共角色
                    if (item.power.Power == 2 && !is_public)
                        continue;

                    // 是否显示管理员
                    if (item.power.Power == 1 && !is_admin)
                        continue;

                    result.Append(",{\"text\":\"" + item.name + "\", \"areid\":\"" + are.id + "\", \"value\":\"" + item.id + "\",\"areRoleID\":\"" + item.areRoleID + "\"}");
                }
            }


            return "[" + result.ToString() + "]";
        }

        private string node_json( AreRoleNode node , bool is_role , bool is_stop, bool is_public, bool is_admin )
        {
            StringBuilder result = new StringBuilder();
            result.Append("{\"text\":\"" + node.are.name + "\", \"areid\":\"" + node.are.id + "\", \"value\":\"-1\" ");
            result.Append(", \"children\":[");

            // 有角色节点,并且需要显示角色
            if (node.roles.Count > 0 && is_role == true )
            {
                foreach (are_role_table item in node.roles)
                {
                    // 是否显示停用角色
                    if (!item.status && !is_stop)
                        continue;

                    // 显示公共角色
                    if (item.power.Power == 2 && !is_public  )
                        continue;

                    // 是否显示管理员
                    if (item.power.Power == 1 && !is_admin)
                        continue;

                    result.Append("{\"text\":\"" + item.name + "\", \"areid\":\"" + node.are.id + "\", \"value\":\"" + item.id + "\",\"areRoleID\":\"" + item.areRoleID + "\"},");
                }

                if (result[result.Length-1] == ',')
                    result.Remove(result.Length - 1, 1);
            }

            result.Append("]");
            result.Append("},");

            return result.ToString();
        }
    }

    public class AreRoleManage
    {
        //  获取区域下的角色
        public static List<Mode.are_role_table> GetRoleByAre(int are)
        {
            DbInstance db = DbManager.GetDbInstance();
            List<Mode.are_role_table> result = new List<are_role_table>();

            string sql = "select " +
                    "J_ID AS areRoleID," +
                    "R_ID AS id, " +
                    "R_NAME AS name, " +
                    "R_POWER AS power, " +
                    "R_REMARK AS remark,  " +
                    "R_DATE AS  date, " +
                    "J_STATUS as status  "+
                    "from are_role_view " +
                    "where A_ID =" + are;

            DataView table = db.ExecuteDataTableBySql(sql).DefaultView;
            for (int index = 0; index < table.Count; index++)
            {
                try
                {
                    are_role_table tmp = new are_role_table();
                    tmp.id = table[index]["id"].ToInt();
                    tmp.name = table[index]["name"].ToString();
                    tmp.power = new POWER(table[index]["power"].ToInt());
                    tmp.remark = table[index]["remark"].ToString();
                    tmp.areRoleID = table[index]["areRoleID"].ToInt();
                    tmp.status = table[index]["status"].ToBool();
                    result.Add(tmp);
                }
                catch (System.Exception ex)
                {
                    throw new Exception(ex.Message +
                      "\nTALBE : [ROLE_TABLE]\n" +
                      "ID: [" + table[index]["id"].ToString() + "]\n" +
                      "NAME: [" + table[index]["name"].ToString() + "]\n" +
                      "POWER:[" + table[index]["power"].ToString() + "]");
                }
            }
            return result;
        }

        //  获取自已的区域信息
        public static Mode.are_table GetSelfAre(int are)
        {
            are_table result = null;
            DbInstance db = DbManager.GetDbInstance();
            DataView view = db.ExecuteDataTableBySql("SELECT * FROM ARE_TABLE WHERE ID = " + are).DefaultView;
            if(view.Count > 0)
            {
                result = new are_table();
                result.id = view[0]["id"].ToInt();
                result.name = view[0]["name"].ToString();
                result.level  =view[0]["level"].ToInt();
            }

            return result;
        }

    }
}
